clear
clear all
cap log close

log using "log-files/2_Figure_A2.smcl", replace smcl

timer on 2

*------------*
* Figure A.2 *
*------------*
	* Figure A.2.a
	*-------------
		use "data/outputs/computo_verificadortrepdate_nomissings.dta", clear
		keep NumMesa trep_date

		merge 1:1 NumMesa using "data/outputs/computo_ulttranstrepdate_nomissings.dta", ///
			keepusing(trep_date_ulttrans)
		drop _merge

		local x = tc(20oct2019 19:40:57)
		local x2 = tc(20oct2019 20:06:59)
		local y = tc(21oct2019 14:56:00)
		local ymin = tc(20oct2019 05:21:28)
		local ywebsite = tc(21oct2019 18:29:53)
		local ymax = tc(21oct2019 21:44:53)

		local xtick0 = tc(20oct2019 8:00:00)
		local xtick1 = tc(20oct2019 16:00:00)
		local xtick2 = tc(20oct2019 23:59:00)
		local xtick3 = tc(21oct2019 8:00:00)

		local oct22nd = tc(22oct2019 00:00:01)
		
		drop if trep_date_verificador >= `oct22nd' & trep_date_verificador != .
		drop if trep_date_ulttrans >= `oct22nd' & trep_date_ulttrans != .

		twoway (scatter trep_date_verificador trep_date_ulttrans if ///
					trep_date_ulttrans < `ywebsite', ///
					msize(vtiny) mcolor(midblue)) ///
			(line trep_date_ulttrans trep_date_ulttrans if ///
				trep_date_ulttrans < `ywebsite', lcolor(gs6)), ///
				graphregion(color(white)) ///
			aspectratio(1) ///
			text(`x2' `=`xtick0'+4000000' "8:06:59 p.m.", fcolor(white) box bcolor(white)) ///
			text(`y' `=`xtick0'+10000' "2:56 p.m.", fcolor(white) box bcolor(white)) ///
			yline(`x2', lcolor(black) lpattern(dash)) ///
			xlabel(`xtick0' `" "10/20" "8:00 a.m." "' ///
				   `xtick1' `" "10/20" "4:00 p.m." "' ///
				   `xtick2' `" "10/21" "12:00 a.m." "' /// 
				   `xtick3' `" "10/21" "8:00 a.m." "', labsize(small) ) /// 
			ylabel(`x2' "10/20" ///
				   `y'  "10/21", angle(0) nogrid) ///
			ytitle("Verification Time") ///
			xtitle("Transmission Time") ///
			xsize(5) ysize(5) ///
			yscale(range(`ymin' `ywebsite' )) ///
			legend(symxsize(*.25) lab(2 "45°")  pos(5) ring(0) order(2)) 
		graph export "outputs/generated/Figure_A2a.png", replace

	* Figure A.2.b
	*-------------
		use "data/outputs/computo_ulttranstrepdate_nomissings.dta", clear

		keep NumMesa trep_date_ulttrans pcs_so
		rename trep_date_ulttrans trep_date_ult_trans_date
		rename pcs_so pcs_so_ult_trans_date

		tempfile ult
		save `ult'

		* Use data set with problem booths appended at the end
		use "data/outputs/computo_verificadortrepdate_nomissings.dta", clear
		keep NumMesa trep_date_verificador pcs_so trep_missing_date

		merge 1:1 NumMesa using `ult'
		drop _merge
		sort trep_date_verificador

		keep NumMesa trep_date_verificador trep_date_ult_trans_date

		gen t = 1
		bys t (trep_date_verificador): gen cdf_verificador = _n if trep_date_verificador != .
		count if trep_date_verificador != .
		local N = r(N)
		replace cdf_verificador = cdf_verificador / `N'

		bys t (trep_date_ult_trans_date): gen cdf_transmision = _n if trep_date_ult_trans_date != .
		count if trep_date_ult_trans_date != .
		local N = r(N)
		replace cdf_transmision = cdf_transmision / `N'

		gen t2=_n
		gen dif = abs(trep_date_verificador - tc(20oct2019 20:06:59))
		sort dif
		sum cdf_verificador if _n==1
		local cutoff = `r(mean)'
		sort t2
		drop t2 dif

		twoway (scatter cdf_verificador cdf_transmision, msize(vtiny) mcolor(midblue)), ///
			graphregion(color(white)) legend(off) xtitle("Proportion of Tally Sheets Transmitted") ///
			ytitle("Proportion of Tally Sheets Verified") aspectratio(1) ///
			text(`cutoff' 0.125 "8:06:59 p.m.", fcolor(white) box bcolor(white)) ///
			ylabel(0 .2 .4 .6 .8 `cutoff' ".96" 1, angle(0) nogrid) ///
			xsize(5) ysize(5) ///
			yline(`cutoff', lpattern(dash) lcolor(black)) 
		graph export "outputs/generated/Figure_A2b.pdf", replace

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
